﻿using System;
using CzfSDK;
using System.Collections.Generic;
using MySql.Data.MySqlClient;
using System.Collections;

namespace Server
{
	public static class FormationManager
	{
//		public const int MAX_FORMATION_COUNT = 20;

		// 创建阵容
		public static int CreateFormation(int userId, string name, string skillIds, int roleId)
		{
//			var all = GetAllFormation (userId);
//			if(all.Count >= MAX_FORMATION_COUNT)
//				throw new CzfProtoException(Constants.ERR3, "阵容数量达到上限");
			CheckFormation (userId, skillIds, roleId);
			return Create (userId, name, skillIds, roleId);
		}

		// 获取玩家的全部阵容
		public static List<Hashtable> GetAllFormation(int userId)
		{
			return GetAll (userId);
		}

		// 修改阵容名字
		public static string UpdateFormationName(int formationId, int userId, string name)
		{
			var formation = Get (formationId);
			if (formation == null || formation.GetVal<int>("userid") != userId)
				throw new CzfProtoException (Constants.ERR3, "错误的阵容id");

			UpdateName (formationId, name);
			return "ok";
		}

		// 修改阵容
		public static string UpdateFormation(int formationId, int userId, string skillIds, int roleId)
		{
			var formation = Get (formationId);
			if (formation == null || formation.GetVal<int>("userid") != userId)
				throw new CzfProtoException (Constants.ERR3, "错误的阵容id");

			CheckFormation (userId, skillIds, roleId);
			Update (formationId, userId, skillIds, roleId);
			return "ok";
		}

		// 删除阵容
		public static string DeleteFormation(int formationId, int userId)
		{
			var formation = Get (formationId);
			if (formation == null || formation.GetVal<int>("userid") != userId)
				throw new CzfProtoException (Constants.ERR3, "错误的阵容id");

			var user = UserManager.Get (userId);
			if(user.GetVal<int>("nowformation") == formationId)
				throw new CzfProtoException (Constants.ERR3, "当前正在使用的阵容不能删除");

			Delete (formationId);
			return "ok";
		}

		// 修改当前阵容
		public static string ChangeNowFormation(int formationId, int userId)
		{
			var formation = Get (formationId);
			if (formation == null || formation.GetVal<int>("userid") != userId)
				throw new CzfProtoException (Constants.ERR3, "错误的阵容id");

			UserManager.UpdateNowFormation (userId, formationId);
			return "ok";
		}

		// 检测阵容是否合法
		private static void CheckFormation(int userId, string skillIds, int roleId)
		{
			var formationSkillCount = MConfig.GetGameConfig<int> ("formationSkillCount");
			var hasSkillIds = SkillManager.GetSkillShortIds(userId);
			var hasRoleIds = RoleManager.GetRoleIds(userId);
			// 检测角色
			if (!hasRoleIds.Contains (roleId))
				throw new CzfProtoException (Constants.ERR3, "玩家没有这个角色: skillIds:" + skillIds + ", roleId:" + roleId + ", userid:" + userId);
			
			// 检测技能
			var setSkillIds = new List<int> ();
			int skillCount = 0;
			int bigSkillCount = 0;

//			CzfLog.Info (skillIds);
			foreach (var skillId in skillIds.Split(',')) {
				int id = Convert.ToInt32 (skillId);

				if(id == 0)
					throw new CzfProtoException (Constants.ERR3, "玩家没有这个技能:" + id + ", skillIds:" + skillIds + ", roleId:" + roleId + ", userid:" + userId);
				if (!hasSkillIds.Contains (id))
					throw new CzfProtoException (Constants.ERR3, "玩家没有这个技能:" + id + ", skillIds:" + skillIds + ", roleId:" + roleId + ", userid:" + userId);
				if(setSkillIds.Contains(id))
					throw new CzfProtoException (Constants.ERR3, "重复配置技能:" + id + ", skillIds:" + skillIds + ", roleId:" + roleId + ", userid:" + userId);
				var skillConfig = MConfig.GetSkillTemplate () [id];
				var ascription = skillConfig.GetVal<int> ("ascription");
				var category = skillConfig.GetVal<int> ("category");
				if(ascription != 0 && ascription != roleId)
					throw new CzfProtoException (Constants.ERR3, "技能归属错误:" + id + ", skillIds:" + skillIds + ", roleId:" + roleId + ", userid:" + userId);

				if (category == 3)
					bigSkillCount++;
				else
					skillCount++;

				setSkillIds.Add (id);
			}
			if(bigSkillCount != 1)
				throw new CzfProtoException (Constants.ERR3, "大招数量不对,必须为1个: skillIds:" + skillIds + ", roleId:" + roleId + ", userid:" + userId);
			if(skillCount != formationSkillCount)
				throw new CzfProtoException (Constants.ERR3, "技能数量不对,必须为"+ formationSkillCount+"个: skillIds:" + skillIds + ", roleId:" + roleId + ", userid:" + userId);
		}

		#region DB
		private static int Create(int userId, string name, string skillIds, int roleId)
		{
			var sql = "insert into formation(userid, roleid, name, skills, createtime, updatetime) values(@userid, @roleid, @name, @skills, now(), now()); select @@identity;";
			return MysqlPool.current.ExecInt (sql, 
				new MySqlParameter ("@userid", userId),
				new MySqlParameter ("@roleid", roleId),
				new MySqlParameter ("@name", name),
				new MySqlParameter ("@skills", skillIds)
			);
		}

		private static int UpdateName(int formationId, string name)
		{
			var sql = "update formation set name = @name, updatetime = now() where id = @id";
			return MysqlPool.current.ExecuteNonQuery (sql, 
				new MySqlParameter ("@id", formationId),
				new MySqlParameter ("@name", name)
			);
		}

		private static int Update(int formationId, int userId, string skillIds, int roleId)
		{
			var sql = "update formation set userid = @userid, roleid = @roleid, skills = @skills, updatetime = now() where id = @id";
			return MysqlPool.current.ExecuteNonQuery (sql, 
				new MySqlParameter ("@id", formationId),
				new MySqlParameter ("@userid", userId),
				new MySqlParameter ("@roleid", roleId),
				new MySqlParameter ("@skills", skillIds)
			);
		}

		private static int Delete(int formationId)
		{
			var sql = "delete from formation where id = @id";
			return MysqlPool.current.ExecuteNonQuery (sql, 
				new MySqlParameter ("@id", formationId)
			);
		}

		public static List<Hashtable> GetAll(int userId)
		{
			var sql = "select * from formation where userid = @userid";
			var dt = MysqlPool.current.ExecDataTable (sql,
				new MySqlParameter ("@userid", userId)
			);
			if (dt == null)
				return null;
			else
				return dt.ToList ();
		}

		public static Hashtable Get(int formationId)
		{
			var sql = "select * from formation where id = @id";
			var dr = MysqlPool.current.ExecDataRow (sql,
				new MySqlParameter ("@id", formationId)
			);
			if (dr == null)
				return null;
			else
				return dr.ToHashtable ();
		}
		#endregion
	}
}

